package CCRMClient.GUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import net.proteanit.sql.DbUtils;
import CCRMClient.ClientStart;


public class SalesReportScreen  extends JPanel{
	private ArrayList<ResultSet> report;
	private JLabel lblSalesReport;
	private JScrollPane scpSalesReport;//for first query
	private JTable tblSalesReport;
	
	private JScrollPane scpSalesReport1;// for second query
	private JTable tblSalesReport1;
	private JButton btnGoBack;
	
/**
 * Create panel
 * 
 */
	
	public SalesReportScreen() {
		initialize();
	}
	
	private void initialize() {
		
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setBackground(Color.WHITE);
		setSize (637, 483);
		setLayout(null);
		add(getLblSalessReport());
		add(getScpSalesReport());
		add(getScpSalesReport1());
		add(getBtnGoBack());
	}
	
	public SalesReportScreen(ArrayList<ResultSet> myrs) {
		this();
		this.report=myrs;
		loadDataToScreen(myrs);
	}
	
	private void loadDataToScreen(ArrayList<ResultSet> myrs){
		ResultSet rs = myrs.get(0);
		DefaultTableModel dtm = (DefaultTableModel) tblSalesReport.getModel();
		dtm.removeRow(0);
		String tbl1Row[] = new String[5];
		try {
			rs.first();
			tbl1Row[0] = rs.getString(1);
			tbl1Row[1] = rs.getString(2);
			tbl1Row[2] = rs.getString(3);
			tbl1Row[3] = rs.getString(4);
			tbl1Row[4] = rs.getString(5);
			dtm.addRow(tbl1Row);
			
			dtm = (DefaultTableModel) tblSalesReport1.getModel();
			dtm.removeRow(0);
			String tbl2Row[] = new String[1];
			rs = myrs.get(1);
			rs.beforeFirst();
			while (rs.next()) {
				tbl2Row[0] = rs.getString(1);
				dtm.addRow(tbl2Row);
			}
		} catch (SQLException e) {}
	}
	
	JLabel getLblSalessReport() {
		if (lblSalesReport == null) {
			lblSalesReport = new JLabel("Sales Report");
			lblSalesReport.setHorizontalAlignment(SwingConstants.CENTER);
			lblSalesReport.setForeground(Color.RED);
			lblSalesReport.setFont(new Font("David", Font.BOLD, 26));
			lblSalesReport.setBackground(Color.WHITE);
			lblSalesReport.setBounds(0, 0, 637, 49);
		}
		return lblSalesReport;
	}
	
	JButton getBtnGoBack() {
		if (btnGoBack == null) {
			btnGoBack = new JButton("Go Back");
			btnGoBack.addActionListener(new BtnGoBackActionListener());
			btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnGoBack.setBounds(37, 394, 124, 23);
		}
		return btnGoBack;
	}
	/**
	 * This inner class implements event listener for pressing Go Back button
	 * @author Ola
	 *
	 */
	
	private class BtnGoBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			ClientStart.panelSwicher(new ReportsScreen());
		}
	}
	JScrollPane getScpSalesReport () {
		if (scpSalesReport == null) {
			scpSalesReport = new JScrollPane();
			scpSalesReport.setBounds(10, 53, 587, 67);
			scpSalesReport.setViewportView(getTblSalesReport());
		}
		return scpSalesReport;
	}
	
	JTable getTblSalesReport() {
		if (tblSalesReport == null) {
			tblSalesReport = new JTable();
			tblSalesReport.setRowSelectionAllowed(false);
			tblSalesReport.setFont(new Font("David", Font.BOLD, 11));
			tblSalesReport.setModel(new DefaultTableModel(
				new Object[][] {
					{null, null, null, null, null},
				},
				new String[] {
					"ID", "Name", "Number Of Customers", "Buys", "Duration"
				}
			) {
				Class[] columnTypes = new Class[] {
					String.class, String.class, String.class, String.class, String.class
				};
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
				boolean[] columnEditables = new boolean[] {
					false, false, false, false, false
				};
				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			});
			tblSalesReport.getColumnModel().getColumn(0).setPreferredWidth(36);
			tblSalesReport.getColumnModel().getColumn(1).setPreferredWidth(104);
			tblSalesReport.getColumnModel().getColumn(2).setPreferredWidth(128);
		}
		return tblSalesReport;
	}
	JScrollPane getScpSalesReport1 () {
		if (scpSalesReport1 == null) {
			scpSalesReport1 = new JScrollPane();
			scpSalesReport1.setBounds(10, 150, 587, 233);
			scpSalesReport1.setViewportView(getTblSalesReport1());
		}
		return scpSalesReport1;
	}
	JTable getTblSalesReport1() {
		if (tblSalesReport1 == null) {
			tblSalesReport1 = new JTable();
			tblSalesReport1.setRowSelectionAllowed(false);
			tblSalesReport1.setFont(new Font("David", Font.BOLD, 11));
			tblSalesReport1.setModel(new DefaultTableModel(
				new Object[][] {
					{null, null, null, null},
				},
				new String[] {
					"Customer refuse"
				}
			) {
				Class[] columnTypes = new Class[] {
					String.class

				};
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
				boolean[] columnEditables = new boolean[] {
					false

				};
				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			});
		}
		return tblSalesReport1;
	}
}
